﻿ 
using Microsoft.EntityFrameworkCore;
using System;
using System.IO.Compression;
using System.Text.Json;
using WebApplication1.Data;
using WebApplication1.Models;
using WebApplication1.Services;

namespace WebApplication1.Services
{
    public class FileService : IFileService
    {



       
        public async Task<DownloadResult> DownloadBatchFilesAsync(List<dynamic> fileIds)
        {
            if (fileIds == null || fileIds.Count == 0)
                return new DownloadResult { Success = false, Message = "没有选择文件" };

            


            // 创建ZIP文件
            var zipFileName = $"batch_download_{DateTime.Now:yyyyMMddHHmmss}.zip";
            var zipFilePath = Path.Combine(Path.GetTempPath(), zipFileName);

            try
            {
                using (var zipArchive = new ZipArchive(new FileStream(zipFilePath, FileMode.Create), ZipArchiveMode.Create))
                {
                    foreach (var file in fileIds)
                    {
                      
                        string jsonString = file.ToString();
                        dynamic fileData = JsonSerializer.Deserialize<dynamic>(jsonString);

                        string guid = fileData.GetProperty("GUID").GetString();
                        string pound_no = fileData.GetProperty("POUND_NO").GetString();


                        var filePath = await GetDownPath(pound_no);

                        filePath = Path.Combine(filePath, guid + ".pdf");
                        

                        var entryName = guid+".pdf";
                        zipArchive.CreateEntryFromFile(filePath, entryName);
                    }
                }

                return new DownloadResult
                {
                    Success = true,
                    Message = $"成功打包 {fileIds.Count} 个文件",
                    FileName = zipFileName
                };
            }
            catch (Exception ex)
            {
                return new DownloadResult { Success = false, Message = $"创建ZIP文件失败: {ex.Message}" };
            }
        }


        /// <summary>
        /// 根据磅单号获取文件下载路径；
        /// </summary>
        /// <param name="pound_no"></param>
        /// <returns></returns>
        public async Task<string> GetDownPath(string pound_no)
        {
            string filePath = "";
            //新能装备质保书地址
            if (pound_no.Length == 10 && pound_no.Substring(0, 2) == "CW")
            {   //合同号分解；
                filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Pdf_Files", "_files_xn", pound_no.Substring(2, 2), pound_no.Substring(4, 1));
            }
            return filePath;
        }

        public async Task<string> GetViewPath(string pound_no)
        {
            string filePath = "";
            //新能装备质保书地址
            if (pound_no.Length == 10 && pound_no.Substring(0, 2) == "CW")
            {   //合同号分解；
                filePath = Path.Combine("_files_xn", pound_no.Substring(2, 2), pound_no.Substring(4, 1)).Replace("\\","/");
            }
            return filePath;
        }



    }
}